<?php
/**
 * Created by PhpStorm.
 * User: l
 * Date: 2016/7/18
 * Time: 15:18
 */
use app\assets\AppAsset;
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use yii\captcha\Captcha;

AppAsset::register($this);

$base =  Yii::$app->request->baseUrl;
AppAsset::addCss($this, $base . "/css/register.css");
$this->title = '注册';
?>
<div id="layout">
    <?php $form = ActiveForm::begin([
        'id' => 'login-form',
        'options' => ['class' => 'form-horizontal'],
        'fieldConfig' => [
            'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
            'labelOptions' => ['class' => 'col-lg-1 control-label'],
        ],
    ]); ?>
        <?= $form->field($model, 'username')->textInput(['autofocus' => true,'placeholder'=>"请输入用户名",]) ?>
        <?= $form->field($model, 'password')->passwordInput(['placeholder'=>"请输入密码"])?>
        <?= $form->field($model, 'password2')->passwordInput(['placeholder'=>"请再次输入密码"])?>
        <?= $form->field($model, 'mobile')->textInput(['placeholder'=>"请输入手机号",'id'=>'tel']) ?>
        <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
            'template' => '<div class="row"><div class="col-lg-3">{image}</div>
                           <div class="col-lg-6"><input type="text" id="verify" class="form-control" placeholder="请输入验证码"></div></div>',
            'captchaAction'=>'index.php/site/captcha',
            'imageOptions'=>['title'=>'换一个', 'alt'=>'换一个', 'style'=>'cursor:pointer;margin-left:25px;']
        ]) ?>
        <?= $form->field($model, 'code')->textInput(['placeholder'=>"请输入验证码"]) ?>
        <span><button id="sms-btn" type="button" class="btn btn-success pull-right" >免费发送验证码</button></span>
        <div align="center"  class="reg_btn" style="margin-bottom: 100px;">
            <?= Html::submitButton('注册', ['class' => 'submit', 'type' => 'button']) ?>
        </div>

    <?php ActiveForm::end(); ?>
</div>
<script type="text/javascript">
    $('#sms-btn').click(function(){
        var verifyCode = $('#verify').val();
        var tel = $.trim($('#tel').val());


        var myReg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;

        if(!myReg.test(tel)){
            alert("请输入正确的手机号");
            return;

        }
        $.ajax({
            type : "POST",
            url :"sms",
            data:{
                'code':verifyCode,
                'tel':tel

            },
            success:function(da){
                console.log('sasas',da);

                var long = 60;
                var btn = $('#sms-btn');
                var res = "\n"+"验证码不正确"
                if(da==res){
                    alert(da);
                }else {
                    btn.prop({disabled:true});
                    btn.removeClass('btn-success');
                    btn.html(long+"s后重新发送");
                    var timer = setInterval(function(){
                        if(long>0){
                            long--;
                            btn.html(long+"s后重新发送");
                        }else{
                            btn.html('重新发送');
                            btn.prop({disabled:false});
                            btn.addClass('btn-success');
                            clearInterval(timer);
                        }
                    },1000);
                }

            }
        });




//        $.post('/distribution/basic/web/index.php/web/sms', {'tel':tel},function(res){
//            console.log(res);
//        });

    });
</script>
